package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.activity2.circle.CircleRankRewardTemplate;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ActivityCircleRankListResponse extends Response{
	
	private List<CircleRankRewardInfo> tempList;
	private List<RankPlayerInfo> rankList;
	private int rank;
	private int score;
	public static class CircleRankRewardItem{
		public int itemId;
		public int count;
		public CircleRankRewardItem(int itemId, int count){
			this.itemId = itemId;
			this.count = count;
		}
	}
	
	public static class CircleRankRewardInfo{
		public int rankFrom;
		public int rankTo;
		public int money;
		public int stone;
		public List<CircleRankRewardItem> items;
		public CircleRankRewardInfo(CircleRankRewardTemplate temp){
			this.rankFrom = temp.getRank();
			this.rankTo = this.rankFrom;
			this.money = temp.getMoney();
			this.stone = temp.getStone();
			items = new ArrayList<CircleRankRewardItem>();
			if(temp.getItemId() != null && temp.getItemId().length > 0){
				for(int i = 0; i < temp.getItemId().length; i++){
//					CircleRankRewardItem item = new CircleRankRewardItem(App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, temp.getItemId()[i]), temp.getItemCount()[i]);
					CircleRankRewardItem item = new CircleRankRewardItem(temp.getItemId()[i], temp.getItemCount()[i]);
					items.add(item);
				}
			}
		}
	}
	public static class RankPlayerInfo{
		public long pId;
		public String name;
		public String img;
		public long score;
		
		public RankPlayerInfo(Player player, long score){
			pId = player.getId();
			name = player.getName();
			img = player.getIcon();
			this.score = score;
		}
	}
	
	public ActivityCircleRankListResponse(int serial, int rank, int score, List<Ranking> players, int off, int len, List<CircleRankRewardTemplate> rewardList) {
		super(serial);
		this.setRank(rank);
//		List<CircleRankRewardTemplate> rewardList = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(CircleRankRewardTemplate.class);
		tempList = new ArrayList<CircleRankRewardInfo>();
		int lastrank = -1;
		int count = 0;
		for(int i = 0; i < rewardList.size(); i++){
			CircleRankRewardTemplate temp = rewardList.get(i);
			if(lastrank > -1){
				tempList.get(count - 1).rankTo = temp.getRank() - 1;
			}
			lastrank = temp.getRank();
			tempList.add(new CircleRankRewardInfo(temp));
			count++;
		}
		rankList = new ArrayList<RankPlayerInfo>();
		PlayerCacheService as = App.getApp().getServiceManager().get(PlayerCacheService.class);
		if(players != null && !players.isEmpty()){
			if(off + len > players.size()){
				len = players.size() - off;
			}
			for(int i = off, to = off + len; i < to; i++){
				Ranking ranking = players.get(i);
				Player actor = as.find(ranking.getPlayerId());
				if(actor != null){
					RankPlayerInfo pInfo = new RankPlayerInfo(actor, ranking.getValue());
					rankList.add(pInfo);
				}
			}
		}
		tempList.get(tempList.size() - 1).rankTo = -1;
		this.score = score;
	}

	public List<CircleRankRewardInfo> getTempList() {
		return tempList;
	}
	
	public void setTempList(List<CircleRankRewardInfo> tempList) {
		this.tempList = tempList;
	}
	
	public List<RankPlayerInfo> getRankList() {
		return rankList;
	}

	public void setRankList(List<RankPlayerInfo> rankList) {
		this.rankList = rankList;
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
}
